package com.oocl.euc.core.maintenance.repository;

import com.oocl.euc.core.builder.RoleAuthBuilder;
import com.oocl.euc.core.builder.RoleBuilder;
import com.oocl.euc.core.builder.UsersBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Role;
import com.oocl.euc.core.maintenance.model.RoleAuth;
import com.oocl.euc.core.maintenance.model.Users;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;

public class RoleRepositoryIT extends ITTestBase {

    private static final String TEST_ROLE_NAME = "Test Role Name";
    @Autowired UsersBuilder usersBuilder;
    @Autowired RoleAuthBuilder roleAuthBuilder;
    @Autowired RoleBuilder roleBuilder;
    @Autowired RoleRepository roleRepository;
    @Autowired UsersRepository usersRepository;
    @Autowired RoleAuthRepository roleAuthRepository;

    private List<String> usersIdList = new ArrayList<>();

    @After
    public void reset(){
        roleRepository.deleteAll();
        roleAuthRepository.deleteAll();
        for(String id : usersIdList) {
            usersRepository.deleteById(id);
        }
        usersIdList.clear();
    }

    @Test
    public void should_find_roles_when_given_valid_staff_id_and_exist_related_roleAuth_information() {
        Users user = usersBuilder.defaultBuilder()
            .withStaffId("1")
            .saveToDB();
        usersIdList.add(user.getStaffId());
        RoleAuth roleAuth = roleAuthBuilder.defaultBuilder()
            .withUsers(user)
            .create();
        Set<RoleAuth> roleAuthSet = new HashSet<RoleAuth>(){{add(roleAuth);}};
        Role role = roleBuilder.defaultBuilder()
            .withRoleAuths(roleAuthSet)
            .withRoleName(TEST_ROLE_NAME)
            .saveToDB();

        List<Role> roles = roleRepository.findRoleByStaffId(user.getStaffId());
        assertEquals(1, roles.size());
        assertEquals(role.getId(), roles.get(0).getId());
        assertEquals(TEST_ROLE_NAME, roles.get(0).getRoleName());
    }

    @Test
    public void should_not_find_roles_when_given_valid_staff_id_but_not_exist_related_roleAuth_information() {
        Users user = usersBuilder.defaultBuilder()
            .withStaffId("1")
            .saveToDB();
        usersIdList.add(user.getStaffId());
        roleBuilder.defaultBuilder()
            .withRoleName(TEST_ROLE_NAME)
            .saveToDB();

        List<Role> roles = roleRepository.findRoleByStaffId(user.getStaffId());
        assertEquals(0, roles.size());
    }

    @Test
    public void should_not_find_roles_when_given_invalid_staff_id() {
        Users user = usersBuilder.defaultBuilder()
            .withStaffId("1")
            .withDomainId("XIEMI2")
            .saveToDB();
        usersIdList.add(user.getStaffId());
        RoleAuth roleAuth = roleAuthBuilder.defaultBuilder()
            .withUsers(user)
            .create();
        Set<RoleAuth> roleAuthSet = new HashSet<RoleAuth>(){{add(roleAuth);}};
        roleBuilder.defaultBuilder()
            .withRoleAuths(roleAuthSet)
            .withRoleName(TEST_ROLE_NAME)
            .saveToDB();

        List<Role> roles = roleRepository.findRoleByStaffId("*******");
        assertEquals(0, roles.size());
    }
}
